<?php
/** [栏目控制器]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-09 16:59:21
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-02-23 11:15:46
 */
class CategoryAction extends PublicAction{

	public $logic;

	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		parent::_initialize();
		$this->logic = D('Category','Logic');
		
	}

	/**
	 * [index 所有栏目]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 读取栏目
		$data = $this->logic->get_all();
		$this->assign('data',$data);

		$this->display();
	}

	/**
	 * [add 添加栏目]
	 */
	public function add()
	{
		if(IS_POST)
		{
			if(!$this->logic->add_category())
				$this->error($this->logic->getError());
			$this->success('栏目添加成功',U('Category/index'));
			die;
		}
		$this->assign('model',D('Model','Logic')->get_all());
		$this->assign('type',D('Type','Logic')->get_all());
		$this->assign('parent',D('Category','Logic')->find_one());
		$this->display();
	}

	/**
	 * [update_cache 更新缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		$this->logic->update_cache();
		$this->success('缓存更新成功',U('Category/index'));
	}


	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{
		if(IS_POST)
		{
			if(!$this->logic->update_category())
				$this->error($this->logic->getError());
			$this->success('栏目编辑成功',U('Category/index'));
			die;
		}


		$field = $this->logic->find_one();
		if(!$field) $this->error('链接错误');
		$this->assign('field',$field);

		$this->assign('type',D('Type','Logic')->get_all());
		$this->display();
	}

	/**
	 * [ajax_del_attachment 删除附件]
	 * @return [type] [description]
	 */
	public function ajax_del_attachment()
	{
		if(!IS_AJAX) return $this->error('链接错误');
		if(!$this->logic->del_attachment())
			$this->ajaxReturn(array('status'=>0,'message'=>$this->logic->getError()));
		else
			$this->ajaxReturn(array('status'=>1,'message'=>'删除成功'));
	}


	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{
		if(!$this->logic->del_category())
			$this->error($this->logic->getError());
		$this->success('栏目删除成功',U('Category/index'));
	}


	/**
	 * [operation 更新栏目操作]
	 * @return [type] [description]
	 */
	public function operation()
	{
		if(!$msg = $this->logic->operation())
			$this->error($this->logic->getError());
		$this->success($msg,U('Category/index'));
	}
	

}